package com.yuandengta.yomall.ware.service.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.yuandengta.common.utils.R;
import com.yuandengta.yomall.ware.feign.MemberFeignService;
import com.yuandengta.yomall.ware.service.FareService;
import com.yuandengta.yomall.ware.vo.FareVO;
import com.yuandengta.yomall.ware.vo.MemberAddressVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 物流运费服务 {@link FareServiceImpl}
 *
 * @author Kevin
 * @email: 178676392@qq.com
 */
@Slf4j
@Service
public class FareServiceImpl implements FareService {

    @Autowired
    private MemberFeignService memberFeignService;

    /**
     * 计算物流运费
     * @param addrId
     * @return
     */
    @Override
    public FareVO getFare(Long addrId) {
        FareVO fareVO = new FareVO();
        // 远程查询收货地址信息
        R r = memberFeignService.addrInfo(addrId);
        if (r.getCode() == 0) {
            MemberAddressVO address = r.getData("memberReceiveAddress", new TypeReference<MemberAddressVO>() {
            });
            if (address != null) {
                fareVO.setAddress(address);
                // 实际物流运费计算需要调用第三方接口（快递，物流）
                String fare = address.getPhone().substring(1,2);
                fareVO.setFare(new BigDecimal(fare));
                return fareVO;
            }
        } else {
            log.error("调用远程服务 yomall-member 查询会员收获地址出现异常");
        }
        return null;
    }
}
